﻿#include "stdafx.h"
#include "AllocateDM.h"

#include "resource.h"

void InitAllocateDM()
{
	//m_GlobalDM = NULL;
	//for (int i = 0; i < 20; i++)
	//{
	//	m_DM[i] = NULL;
	//}
}

//注册全局大漠
BOOL RegisterGlobalDM(CString strDMPath)
{
	if (::OleInitialize(NULL) == S_OK)							//初始化COM库
	{
		HMODULE hLib = LoadLibrary(strDMPath);
		if (hLib < (HMODULE)HINSTANCE_ERROR)
		{
			AfxMessageBox(L"加载大漠dll失败,或者尝试手动注册大漠!");
			return FALSE;

		}
		TRACE(L"[sdn] Reg 11111");
		FARPROC lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
		if (lpDllEntryPoint == NULL)
		{
			AfxMessageBox(L"找不到注册入口,或者尝试手动注册大漠!");
			return FALSE;
		}

		if (FAILED((*lpDllEntryPoint)()))
		{
			AfxMessageBox(L"注册大漠插件失败！尝试手动注册！参见帮助文档!");
			return FALSE;
		}

		FreeLibrary(hLib);
		TRACE(L"[sdn] Reg 2222");
		//根据给定的程序标示符从注册表中找出对应的类标示符
		//这里的"dm.dmsoft"是dll在注册表里的键值，将类标示符赋给clsid 
		//hr = CLSIDFromProgID(OLESTR("dm.dmsoft"), &clsid);
		//用指定的类标示符创建一个未初始化的COM对象
		//Idmsoft：创建的com对象的接口标示符
		//dm:用来接收指向com对象接口地址的指针变量
		//CLSCTX_INPROC_SERVER  创建同一进程中运行的组件
		//Hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, __uuidof(Cdmsoft), (LPVOID*)&dm);

		//if (SUCCEEDED(Hr))
		//{
		//	CoFreeUnusedLibraries();				//卸载不用的COM服务
		//}
		//else
		//{
		//	AfxMessageBox("创建大漠对象失败！尝试手动注册！参见帮助文档！");
		//	return;
		//}
		m_GlobalDM = new Cdmsoft;
		m_OnLineDM = new Cdmsoft;
		if (m_GlobalDM == NULL)
		{
			AfxMessageBox(L"分配大漠对象内存失败！尝试清理内存！");
			::OleUninitialize();
			return FALSE;
		}
		if (m_GlobalDM->CreateDispatch(L"dm.dmsoft") == FALSE)
		{
			AfxMessageBox(L"创建大漠对象失败！尝试手动注册！参见帮助文档！");
			::OleUninitialize();
			return FALSE;
		}
		if (m_OnLineDM->CreateDispatch(L"dm.dmsoft") == FALSE)
		{
			AfxMessageBox(L"创建大漠对象失败！尝试手动注册！参见帮助文档！");
			::OleUninitialize();
			return FALSE;
		}
		//检测大漠版本是否正确
		//if (m_GlobalDM->Ver() != "6.1550")
		//{
		//	CString tmp;
		//	tmp.Format("版本错误，当前版本号是:%s", m_GlobalDM->Ver());
		//	MessageBox(NULL, tmp, "错误", MB_OK);
		//	::OleUninitialize();
		//	return FALSE;
		//}
		TRACE(L"[sdn] Reg 3333");
		// 步骤5 收费用户注册. 如果是使用免费功能,可以略过这一步
		CString result;
		result.LoadString(IDS_STRING_DM);
		TRACE(L"[sdn] Reg 4444");
		long ret = m_GlobalDM->Reg((_bstr_t)result, L"");//
		if (ret != 1)
		{
			CString tmp;
			tmp.Format(L"大漠收费用户注册失败,返回值是:%d", ret);
			MessageBox(NULL, tmp, L"错误", MB_OK);
			::OleUninitialize();

			return FALSE;
		}

		TRACE(L"[sdn] Reg 55555");
		//初始化大漠插件全局路径和字体位置
		char pBuf[MAX_PATH];
		TCHAR *path = new TCHAR[MAX_PATH];
		ZeroMemory(path, MAX_PATH);
		// path == "d:\Project\Test\MFC\MFC\debug"
		GetCurrentDirectory(MAX_PATH, path);
		CString ver = m_GlobalDM->Ver();
		CString m_dmpath = path;
		m_GlobalDM->SetPath(m_dmpath+"\\pic");
		m_OnLineDM->SetPath(m_dmpath + "\\pic");

		//m_GlobalDM->SetDict(0, "dm_lv.txt");//设置字库路劲
		//m_GlobalDM->SetDict(1, "dm_game.txt");
		//m_GlobalDM->SetDict(2, "dm_gold.txt");
		//m_GlobalDM->SetDict(3, "dm_server_login.txt");
		//m_GlobalDM->SetDict(4, "sell_goods.txt");

		//m_GlobalDM->UseDict(1);
		//m_GlobalDM->SetShowErrorMsg(0);		//屏蔽坐标越界提示
		m_OnLineDM->SetShowErrorMsg(0);
		Sleep(500);
		//::OleUninitialize();

	}
	else
	{
		AfxMessageBox(L"初始化COM组件失败!");
		return FALSE;
	}
	return TRUE;
}

//查找一个空闲的大漠序号
int FindFreeDMIndex()
{
	for (int i = 0; i < 20; i++)
	{
		TRACE(L"[sdn] FindFreeDMIndex:%X", m_DM[i]);
		if (m_DM[i] == NULL)
			return i;
	}
	return -1;
}


int GetDmNumber()
{
	int iNumber = 0;
	for (int i = 0; i < 20; i++)
	{
		TRACE(L"[sdn] FindFreeDMIndex:%X", m_DM[i]);
		if (m_DM[i] != NULL)
		{
			iNumber++;
		}

	}
	return iNumber;
}
//注册一个大漠对象
BOOL RegisterDM(int iDmIndex)
{
	TRACE(L"[sdn] 初始化大漠对象");
	if (::OleInitialize(NULL) == S_OK)							//初始化COM库
	{
		m_DM[iDmIndex] = new Cdmsoft;
		if (m_DM[iDmIndex] == NULL)
		{
			AfxMessageBox(L"分配大漠对象内存失败！尝试清理内存！");
			::OleUninitialize();
			return FALSE;
		}
		if (m_DM[iDmIndex]->CreateDispatch(L"dm.dmsoft") == FALSE)
		{
			AfxMessageBox(L"创建大漠对象失败！尝试手动注册！参见帮助文档！");
			::OleUninitialize();
			return FALSE;
		}
		else
		{
			//初始化大漠插件全局路径和字体位置
			char pBuf[MAX_PATH];
			TCHAR *path = new TCHAR[MAX_PATH];
			ZeroMemory(path, MAX_PATH);
			// path == "d:\Project\Test\MFC\MFC\debug"
			GetCurrentDirectory(MAX_PATH, path);
			CString m_dmpath = path;
			m_DM[iDmIndex]->SetPath(m_dmpath);

			//aDm[nDmID]->SetPicPwd("626617"); qiyue
			//aDm[nDmID]->SetDictPwd("626617");
			//m_DM[iDmIndex]->SetDict(0, "dm_lv.txt");
			//m_DM[iDmIndex]->SetDict(1, "dm_game.txt");
			//m_DM[iDmIndex]->SetDict(2, "dm_gold.txt");
			//m_DM[iDmIndex]->SetDict(3, "dm_server_login.txt");
			//m_DM[iDmIndex]->SetDict(4, "sell_goods.txt");

			//m_DM[iDmIndex]->UseDict(1);
			/*aDm[nDmID]->InitCri();
			aDm[nDmID]->EnableDisplayDebug(1);*/

			m_DM[iDmIndex]->SetShowErrorMsg(0);		//屏蔽坐标越界提示
			Sleep(500);

		}
		//::OleUninitialize();
	}
	else
	{
		CString str;
		str.Format(L"大漠ID:%d初始化COM组件失败!", iDmIndex);
		AfxMessageBox(str);
		return FALSE;
	}
	return TRUE;
}

//清理一个大漠对象信息
void ClearOneDM(int iDmIndex)
{

	//CloseHandle((HANDLE)m_ScriptSet[i].m_hTaskThreadHandle);
	if (m_DM[iDmIndex] != 0)
	{
		DbgOut(L"[sdn] 清理一个大漠对象");
		m_DM[iDmIndex]->UnBindWindow();
		delete m_DM[iDmIndex];
		m_DM[iDmIndex] = NULL;

		m_ScriptSet[iDmIndex].m_hGameHandle = NULL;
		m_ScriptSet[iDmIndex].m_iControlIndex = -1;
		m_ScriptSet[iDmIndex].m_iDmIndex = -1;
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strIndex = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strHandle = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strAccount = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strPwd = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strServer = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strMoney = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strLv = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex = 1;
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strRunState = L"";
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_iLoginNum = 0;
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
		//memset((void*)&m_ScriptSet[iDmIndex].m_GameInfoSt, 0, sizeof(GameInfo));
	}


}


//清理所有的大漠对象信息
void ClearAllDM()
{
	for (int i = 0; i < 20; i++)
	{
		int iDmIndex = m_ScriptSet[i].m_iDmIndex;
		if (iDmIndex != -1 && m_DM[iDmIndex] != NULL)
		{
			//CloseHandle((HANDLE)m_ScriptSet[iDmIndex].m_hTaskThreadHandle);

			m_DM[iDmIndex]->UnBindWindow();
			delete m_DM[iDmIndex];
			m_DM[iDmIndex] = NULL;

			m_ScriptSet[iDmIndex].m_hGameHandle = NULL;
			m_ScriptSet[iDmIndex].m_iControlIndex = -1;
			m_ScriptSet[iDmIndex].m_iDmIndex = -1;


			m_ScriptSet[i].m_GameInfoSt.m_strIndex = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strHandle = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strAccount = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strPwd = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strServer = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strMoney = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strLv = L"";
			m_ScriptSet[i].m_GameInfoSt.m_iRoleIndex = 1;
			m_ScriptSet[i].m_GameInfoSt.m_strTaskState = L"";
			m_ScriptSet[i].m_GameInfoSt.m_strRunState = L"";

			m_ScriptSet[i].m_GameInfoSt.m_iLoginNum = 0;
			m_ScriptSet[i].m_GameInfoSt.m_iOutTime_Num = 0;

			//memset((void*)&m_ScriptSet[iDmIndex].m_GameInfoSt, 0, sizeof(GameInfo));
		}
	}
	::OleUninitialize();
}

